<template>
  <el-dialog title="退货确认" width="50%" :visible.sync="visible">
    <el-form ref="form" :model="form" class="dialog-form" label-position="right" label-width="120px">
      <!-- <el-form-item label="回执单" prop="params.receipt" :rules="self.page.rules.def">
        <form-upload-image v-model="form.params.receipt" size="mini" :remote="self.upload.img" />
      </el-form-item>
      <div>
        <el-divider content-position="left">上传凭证 <el-button icon="el-icon-plus" type="text" @click="plus" /></el-divider>
        <el-form-item
          v-for="(item, index) in form.params.certificates"
          :key="item.key"
          :label="'凭证' + (index+1)"
          :prop="'params.certificates.' + index + '.picture'"
        >
          <form-upload-image v-model="item.picture" size="mini" :remote="self.upload.img" />
          <el-button icon="el-icon-delete" type="text" @click="remove(item)" />
        </el-form-item>
      </div> -->
      <el-form-item label="补充描述" prop="message">
        <el-input v-model="form.message" type="textarea" :rows="2" placeholder="请输入补充描述" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="submit">确认</el-button>
    </div>
  </el-dialog>
</template>
<script>

export default {
  name: 'PurReturning',
  props: {
    value: {
      type: String,
      default: ''
    },
    self: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      visible: false,
      form: {
        id: null,
        message: null,
        params: {
          type: 0,
          receipt: '',
          certificates: []
        }
      },
      settleQuantity: 0
    }
  },
  created() {

  },
  methods: {
    show(settleQuantity) {
      this.visible = true
      this.settleQuantity = settleQuantity
    },
    submit() {
      const that = this
      that.form.id = that.value
      that.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        if (that.settleQuantity >= 0 && that.form.params.type === 0) {
          that.$message.error('存在验货货品数据,无法全部退货!')
          return
        }
        if (that.settleQuantity === 0 && that.form.params.type === 1) {
          that.$message.error('请进行收货结算,再进行部分退货!')
          return
        }

        const loading = that.$loading()
        const url = that.self.page.router.remoteUrl + '/returning'
        that.self.service.post(url, that.form).then(res => {
          loading.close()
          if (res.code !== 200) {
            that.$message.error(res.message)
            return
          }
          that.$message.success('操作成功!')
          that.self.reload(true)
          // that.$router.push({ name: 'PurReturnOrderManageEdit', params: { no: res.data }})
        }).catch((e) => {
          loading.close()
        })
      })
    },
    plus() {
      this.form.params.certificates.push({ picture: '', key: Date.now() })
    },
    remove(item) {
      var index = this.form.params.certificates.indexOf(item)
      if (index !== -1) {
        this.form.params.certificates.splice(index, 1)
      }
    }
  }
}
</script>
<style scoped>
.upload-image{
  float: left;
  margin-right: 10px;
}
</style>
